package com.suixun.jxt.service.impl;

import org.jsoup.helper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.entity.jxt.JxtResult;
import com.suixun.entity.jxt.JxtResultList;
import com.suixun.entity.jxt.JxtSchool;
import com.suixun.entity.jxt.JxtTeacherResult;
import com.suixun.entity.jxt.Result0;
import com.suixun.jxt.service.JxtService;
import com.suixun.service.Constants;
import com.suixun.util.HttpUtil;
import com.suixun.util.JWTHelper;

import net.sf.json.JSONObject;

@Service
public class JxtServiceImpl implements JxtService {

	private final static Logger log = LoggerFactory.getLogger(JxtServiceImpl.class);

	private final static String JXT_ADD_BABY_INFO = "/jxt-datamanager/actStudent.do";
	private final static String JXT_MODIFY_BABY_INFO = "/jxt-datamanager/actStudent.do";
	private final static String JXT_JUDGE_TEACHER = "/jxt-datamanager/actTeacher.do";
	private final static String JXT_CANCEL_BABY_INFO = "/jxt-datamanager/actStudent.do";
	private final static String JXT_GET_STUDENT = "/jxt-commonbackground/ajaxStudent.do";
	private final static String JXT_GET_STUDENTS = "/jxt-commonbackground/ajaxStudent.do";
	private final static String JXT_MODIFY_PARENT_MOBILE = "/jxt-commonbackground/ajaxStudent.do";
	private final static String JXT_GET_TEACHERS = "/jxt-commonbackground/ajaxStudent.do";
	private final static String JXT_SCHOOL = "/jxt-commonbackground/ajaxSchool.do";

	private String getToken() {

		JWTHelper jWTHelper = new JWTHelper();
		return jWTHelper.createJWT("8756", "13868113229", "123456", 0l, "guardian");
	}

	@Override
	public JxtResult addBabyInfo(String teachertel, String peopletel, String babyName) {

		String param = "method=ajaxAddStudentFromSuixun";
		if (!StringUtil.isBlank(teachertel)) {
			param += "&teachertel=" + teachertel;
		}
		if (!StringUtil.isBlank(peopletel)) {
			param += "&peopletel=" + peopletel;
		}
		if (!StringUtil.isBlank(babyName)) {
			param += "&babyName=" + babyName;
		}
		param += "&token=" + getToken();

		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_ADD_BABY_INFO, param, false);
			log.info(String.format("addBabyInfo:%s", result));
			ObjectMapper om = new ObjectMapper();
			JxtResult jxtResult = om.readValue(result, JxtResult.class);
			if (jxtResult == null && result instanceof String) {
				JxtResult res = new JxtResult();
				res.setCode(1);
				res.setMsg(result);
				return res;
			}
			return jxtResult;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtResult modifyBabyInfo(Integer jxtBabyId, String teachertel, String babyName, String sex, String headImg) {

		String param = "method=ajaxUpdateStudentInfo";
		if (!StringUtil.isBlank(teachertel)) {
			param += "&teachertel=" + teachertel;
		}
		if (jxtBabyId != null) {
			param += "&studentid=" + jxtBabyId;
		}
		if (!StringUtil.isBlank(babyName)) {
			param += "&studentName=" + babyName;
		}
		if (!StringUtil.isBlank(sex)) {
			param += "&sex=" + sex;
		}
		if (!StringUtil.isBlank(headImg)) {
			param += "&headImg=" + headImg;
		}
		param += "&token=" + getToken();

		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_MODIFY_BABY_INFO, param, false);
			log.info(String.format("modifyBabyInfo:%s", result));
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtTeacherResult judgeTeacher(String teachertel) {

		String param = "method=getTeacherInfByTel&teachertel=" + teachertel + "&token=" + getToken();
		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_JUDGE_TEACHER, param, false);
			log.info(String.format("judgeTeacher:%s", result));
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtTeacherResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtResult cancelStudent(Integer studentId) {

		String param = "method=ajaxDeleteStudentBySuixun&studentid=" + studentId + "&token=" + getToken();
		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_CANCEL_BABY_INFO, param, false);
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtResult getStudentInfo(Integer studentId) {
		String param = "method=ajaxGetStudentInfo&studentid=%s&type=2&token=%s";

		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_GET_STUDENT,
					String.format(param, studentId, getToken()), false);
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtResultList getStudentList(String studentIds) {
		String param = "method=ajaxGetStudentList&studentids=%s&type=2&token=%s";

		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_GET_STUDENTS,
					String.format(param, studentIds, getToken()), false);
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResultList.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtResult modifyBabyParentMobile(String oldmobile, String mobile, Integer jxtBabyId) {

		String param = "method=ajaxModifyBabyParentMobile&oldmobile=%s&mobile=%s&token=%s&jxtBabyId=%d";
		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_MODIFY_PARENT_MOBILE,
					String.format(param, oldmobile, mobile, getToken(), jxtBabyId), false);
			log.info(Constants.getJxtPath() + JXT_MODIFY_PARENT_MOBILE+"?"+String.format(param, oldmobile, mobile, getToken(), jxtBabyId));
			log.info(String.format("modifyParentMobile:%s", result));
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return null;
	}

	@Override
	public JxtResult modifyHeadmasterMobile(String oldmobile, String mobile) {
		String param = "method=ajaxModifyHeadmasterMobile&oldmobile=%s&mobile=%s&token=%s";
		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_MODIFY_PARENT_MOBILE,
					String.format(param, oldmobile, mobile, getToken()), false);
			log.info(Constants.getJxtPath() + JXT_MODIFY_PARENT_MOBILE+"?"+String.format(param, oldmobile, mobile, getToken()));
			log.info(String.format("modifyHeadmasterMobile:%s", result));
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JxtResult getTeachersBySchools(String schoolNames) {
		String param = "method=ajaxGetTeachersBySchools&schoolNames=%s&token=%s";
		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_GET_TEACHERS,
					String.format(param, schoolNames, getToken()), false);
			log.info(Constants.getJxtPath() + JXT_GET_TEACHERS+"?"+String.format(param, schoolNames, getToken()));
			log.info(String.format("getTeachersBySchools:%s", result));
			ObjectMapper om = new ObjectMapper();
			return om.readValue(result, JxtResult.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public JSONObject getSchool(String schoolName) {
		String param = "method=ajaxGetSchoolInfo&schoolName=%s&token=%s";
		try {
			String result = HttpUtil.sendPost(Constants.getJxtPath() + JXT_SCHOOL,
					String.format(param, schoolName, getToken()), false);
			log.info(Constants.getJxtPath() + JXT_SCHOOL+"?"+String.format(param, schoolName, getToken()));
			log.info(String.format("getSchool:%s", result));
			ObjectMapper om = new ObjectMapper();
			Result0 result0 = om.readValue(result, Result0.class);
			if(result0 != null && "0".equals(result0.getCode())) {
				return result0.getData();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

}
